var index = parent.layer.getFrameIndex(window.name);
var task_number = parent.$("#taskNumber").val();
var currentBlockId = parent.$("#blockid").val();
var cpuNum = parent.$("#"+currentBlockId+"cpuNum").val();
var paramsC = currentBlockId+"params";
var paramsC_value = parent.$("#"+paramsC).val();
var paramsCD_json = eval("("+parent.$("#"+paramsC+"Default").val()+")");
var currentAlgorithmId = parent.$("#algorithmid").val();
var frontAlgorithmIds = parent.$("#frontAlgorithmIds").val();
var json_frontAlgorithmIds = eval("("+frontAlgorithmIds+")");
var useMpi = "";
var allowModification = parent.$("#allowModification").val();
window.onload = function () {
    $.ajax({
        url:constUrl+"ws/model",
        type:'post',
        dataType:'json',
        data:{
            "operateType":"configModel",
            "data":JSON.stringify({
                "currentAlgorithmId":currentAlgorithmId,
                "currentBlockId":currentBlockId,
                "frontAlgorithmIds":json_frontAlgorithmIds
            })
        },
        success: function(responseText) {
            var jsonData = responseText;
            $("#task_number").val(task_number);
            creatHtml(jsonData);
            if(paramsC_value!=""&&paramsC_value!=null&&paramsC_value!="undefined"){
                var i = 0;
                var paramsC_json = eval("("+paramsC_value+")");
                $("#paramTabContent").children("div").each(function (index, elem) {
                    var $elem = $(elem);
                    var param_temp = paramsC_json[i];
                    var j = 0;
                    $elem.children(".param").each(function (pindex, pelem) {
                        var $pelem = $(pelem);
                        var temp = param_temp[j].paramValue;
                        $pelem.children("input").val(temp);
                        j++;
                    });
                    i++;
                });
                if(cpuNum!=null&&cpuNum!=""&&cpuNum!=0){
                    $("#cpuParam").val(cpuNum);
                }
            }
            if(allowModification=="false"){
                console.log("aaaaaa");
                $("#y-title").append("--查看模式");
                $(":button").attr("disabled", true);
                $("input[type=text]").attr("readonly", true);
            }
        },
        complete: function() {},
        error: function() {
            layer.alert("获取数据失败！")
        }
    });
    $('#saveSetting').click(function () {
        if(allowModification=="true") {
            var blocks = [];
            var one_block = [];
            $(".tab-pane").each(function (index, elem) {
                var $elem = $(elem);
                var param = $elem.children(".param");
                one_block = [];
                param.each(function (cindex, celem) {
                    var $celem = $(celem);
                    one_block.push({
                        paramName: $celem.children('.param_name').children('label').children(".param_name_text").text(),
                        paramValue: $celem.children('input').val()
                    });
                });
                var json_one_block = JSON.stringify(one_block);
                blocks += "," + json_one_block;
            });
            blocks = blocks.substr(1);
            blocks = "[" + blocks + "]";
            window.parent.document.getElementById(paramsC).value = blocks;
            $("#" + paramsC, window.parent.document).parent().children(".uidiv").children(".progressbar").children("div").attr("style", "width: 100%");
            if (useMpi) {
                $("#" + currentBlockId + "cpuNum", window.parent.document).val($("#cpuParam").val());
            }
            parent.layer.close(index);
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
    $('#closeIframe').click(function(){
        parent.layer.close(index);
    });
    $('#setParams').click(function () {
        if(allowModification=="true") {
            var json_value = paramsCD_json;
            if ($($($("#paramTabContent").children(".tab-pane").get(0)).children(".param").get(0)).children("input").val() != "" && $($($("#paramTabContent").children(".tab-pane").get(0)).children(".param").get(0)).children("input").val() != null) {
                var temp = [];
                var param = $($("#paramTabContent").children(".tab-pane").get(0)).children(".param");
                param.each(function (cindex, celem) {
                    var $celem = $(celem);
                    temp.push({
                        paramName: $celem.children('.param_name').children('label').children(".param_name_text").text(),
                        paramValue: $celem.children('input').val()
                    });
                });
                json_value = temp;
            }
            $("#param_form").children(".table_div").children("#paramTabContent").children(".tab-pane").each(function (index, elem) {
                var $elem = $(elem);
                $elem.children(".param").each(function (cindex, celem) {
                    var $celem = $(celem);
                    for (var i = 0; i < json_value.length; i++) {
                        if ($celem.children(".param_name").children("label").children("i").html() == json_value[i].paramName) {
                            $celem.children("input").val(json_value[i].paramValue);
                        }
                    }
                });
            });
        } else {
            layer.alert("查看模式无法进行该操作！");
        }
    });
}
creatHtml = function(jsonData){
    useMpi = jsonData.currentAlgorithmDetail.useMpi;
    dataId = jsonData.dataId;
    var param_form = document.getElementById("param_form");
    var paramsConfig = jsonData.currentAlgorithmDetail.algorithmParams;
    var ul = "";
    var div = "";
    var div_one = "";
    for(var i=1; i<=task_number; i++){
        ul += '<li role="presentation"><a href="#task_'+i+'" aria-controls="task_'+i+'" role="tab" data-toggle="tab">任务'+i+'</a></li>';
        div_one = "";
        for(var j=0;j<paramsConfig.length;j++){
            div_one +=
                '<hr>' +
                '<div class="param">' +
                    '<div class="form-group param_name"> ' +
                        '<label class="col-sm-2">参数符号:<i class="param_name_text">'+paramsConfig[j].paramSymbol+'</i></label> ' +
                    '</div> ' +
                    '<input type="text" class="form-group form-control param-one" id="Param'+i+'~'+paramsConfig[j].paramSymbol+'" value=""> ' +
                    '<div class="form-group"> ' +
                        '<label class="col-sm-2 control-label">简介</label> ' +
                        '<div class="col-sm-10"> ' +
                            '<p class="form-control-static">'+paramsConfig[j].paramIntroduction+'</p> ' +
                        '</div> ' +
                    '</div>' +
                '</div> ';
        }
        if(useMpi){
            div_one +=
                '<hr>' +
                '<div class="cpuParam">' +
                    '<div class="form-group param_name"> ' +
                        '<label class="col-sm-2">参数符号:<i class="param_name_text">CPU</i></label> ' +
                    '</div> ' +
                    '<input type="tel" class="form-group form-control param-one" id="cpuParam" value="'+jsonData.currentAlgorithmDetail.cpuNum+'"> ' +
                    '<div class="form-group"> ' +
                        '<label class="col-sm-2 control-label">简介</label> ' +
                        '<div class="col-sm-10"> ' +
                            '<p class="form-control-static">CPU核数</p> ' +
                        '</div> ' +
                    '</div>' +
                '</div> ';
        }
        div += '<div role="tabpanel" class="tab-pane fade" id="task_'+i+'"><div class="form-title"><h4>参数配置</h4></div>'+div_one+'</div>';
    }
    ul = '<ul class="nav nav-tabs nav-pills nav-stacked table_div-ul" role="tablist" id="paramTabs">'+ul+'</ul>';
    div = '<div class="tab-content table_div-div" id="paramTabContent">'+div+'</div>';
    param_form.innerHTML += '<div class="table_div">'+ul+div+'</div>';
    $('#paramTabs a').click(function (e) {
        e.preventDefault()
        $(this).tab('show')
    });
    $('a[href="#task_1"]').tab('show');
}